/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


package ihm_clarifiee.Presentation;

import ihm_clarifiee.Presentation.model_table.model_table_listeRelease;
import ihm_clarifiee.Control.CHistoire;
import ihm_clarifiee.Control.CBacklog;
import ihm_clarifiee.Control.CProjet;
import ihm_clarifiee.Control.CRelease;
import ihm_clarifiee.application.Histoire;
import ihm_clarifiee.application.Projet;
import ihm_clarifiee.application.Release;
import ihm_clarifiee.Presentation.popup.base_affichage;
import ihm_clarifiee.Presentation.popup.popup_release;
import ihm_clarifiee.base_affichage_histoire;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.util.Iterator;
import java.util.Locale;
import java.util.ResourceBundle.Control;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.table.DefaultTableModel;
import sun.awt.VerticalBagLayout;

/**
 *
 * @author 28001673
 */
public class PListeReleases extends javax.swing.JPanel {

  //  private CBacklog controle;
    private model_table_listeRelease md;
    private model_table_listeRelease mda;
    private CProjet projet;
    private PListeReleases ptest;
    
    /**
     * 
     * @param p
     */
    public PListeReleases(CProjet p) {
        projet =p;
        initComponents();
        md=new model_table_listeRelease();
        mda=new model_table_listeRelease();
        tbl_principal.setModel(md);
        tbl_active.setModel(mda);
        initModel();
        updateliste();   
    }
    
   public void setListe(PListeReleases pdt){
	   ptest=pdt;
   }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initModel ()
    {
        tbl_principal.getColumnModel().getColumn(0).setMinWidth(20);
        tbl_principal.getColumnModel().getColumn(0).setPreferredWidth(100);
        tbl_principal.getColumnModel().getColumn(0).setMaxWidth(2000);
        tbl_principal.getColumnModel().getColumn(1).setMinWidth(30);
        tbl_principal.getColumnModel().getColumn(1).setPreferredWidth(300);
        tbl_principal.getColumnModel().getColumn(1).setMaxWidth(4500);
        tbl_principal.getColumnModel().getColumn(2).setMinWidth(100);
        tbl_principal.getColumnModel().getColumn(2).setPreferredWidth(150);
        tbl_principal.getColumnModel().getColumn(2).setMaxWidth(4000);
        tbl_principal.getColumnModel().getColumn(3).setMinWidth(100);
        tbl_principal.getColumnModel().getColumn(3).setPreferredWidth(150);
        tbl_principal.getColumnModel().getColumn(3).setMaxWidth(4000);
        tbl_principal.getColumnModel().getColumn(0).setResizable(true);
        tbl_principal.getColumnModel().getColumn(1).setResizable(true);
        tbl_principal.getColumnModel().getColumn(2).setResizable(true);
        tbl_principal.getColumnModel().getColumn(3).setResizable(true);
        tbl_principal.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);

       tbl_active.getColumnModel().getColumn(0).setMinWidth(20);
       tbl_active.getColumnModel().getColumn(0).setPreferredWidth(100);
       tbl_active.getColumnModel().getColumn(0).setMaxWidth(2000);
       tbl_active.getColumnModel().getColumn(1).setMinWidth(30);
       tbl_active.getColumnModel().getColumn(1).setPreferredWidth(300);
       tbl_active.getColumnModel().getColumn(1).setMaxWidth(4500);
       tbl_active.getColumnModel().getColumn(2).setMinWidth(100);
       tbl_active.getColumnModel().getColumn(2).setPreferredWidth(150);
       tbl_active.getColumnModel().getColumn(2).setMaxWidth(4000);
       tbl_active.getColumnModel().getColumn(3).setMinWidth(100);
       tbl_active.getColumnModel().getColumn(3).setPreferredWidth(150);
       tbl_active.getColumnModel().getColumn(3).setMaxWidth(4000);
       tbl_active.getColumnModel().getColumn(0).setResizable(true);
       tbl_active.getColumnModel().getColumn(1).setResizable(true);
       tbl_active.getColumnModel().getColumn(2).setResizable(true);
       tbl_active.getColumnModel().getColumn(3).setResizable(true);
       tbl_active.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);

        
 
     }

  
    private void initComponents() {

        panelVertical = new JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        tbl_principal = new javax.swing.JTable();
        tbl_active = new javax.swing.JTable();
        bt_ajouter = new javax.swing.JButton();
        bt_suppr = new javax.swing.JButton();
        bt_modifier = new javax.swing.JButton();
        bt_detail = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        bt_monter = new javax.swing.JButton();

           
        jScrollPane1.setViewportView(tbl_principal);
        jScrollPane2.setViewportView(tbl_active);
        
        
        bt_ajouter.setText("Ajouter");
        bt_ajouter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_ajouterActionPerformed(evt);
            }
        });

        bt_suppr.setText("Suppr");
  
          bt_suppr.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_supprActionPerformed(evt);
            }
        });
          
          
        bt_modifier.setText("Modifier");

        bt_detail.setText("Detail");
        
          bt_detail.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_detailActionPerformed(evt);
            }
        });
            bt_modifier.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_modifierActionPerformed(evt);
            }
        });
        
        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel1.setText("Liste des releases");
        jLabel1.setMaximumSize(new java.awt.Dimension(146, 124));

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel2.setText("Release Active");
        jLabel2.setMaximumSize(new java.awt.Dimension(146, 124));

        bt_monter.setText("Activer");
        bt_monter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_activerActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(bt_suppr, 98, 98,98)
                            .addComponent(bt_ajouter, 98, 98, 98)
                            .addComponent(bt_modifier, 98, 98, 98)
                            .addComponent(bt_detail, 98, 98, 98)
                            .addComponent(bt_monter, 98, 98, 98)))
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(9, 9, 9)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(bt_ajouter)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bt_modifier)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bt_suppr)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bt_detail)
                        .addGap(23, 23, 23)
                        .addComponent(bt_monter)
                         .addGap(123, 123, 123))
                    .addComponent(jScrollPane1, 0, 0, Short.MAX_VALUE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(9, 9, 9)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jScrollPane2, 35,35, Short.MAX_VALUE)
                .addContainerGap())
        );

    }                      

    private void bt_ajouterActionPerformed(java.awt.event.ActionEvent evt) {                                           
      //  String [] t ={"nom","objectif","date debut","date fin"};
      //  md.add(t);
      //  tbl_principal.repaint();
        popup_release popup = new popup_release(projet);
        base_affichage ba = new base_affichage(popup);
        ba.getMainPanel().setLayout(new VerticalBagLayout());
        ba.getMainPanel().add(popup);
        ba.setVisible(true);
    }                                          

    private void bt_descendreActionPerformed(java.awt.event.ActionEvent evt) {                                             
     
      //   tbl_principal.repaint();
    }
    
      private void bt_supprActionPerformed(java.awt.event.ActionEvent evt) {                                             
      int sel = ligneSelectionnee();
      int n = JOptionPane.showConfirmDialog(null,"Voulez-vous vraiment supprimer cette release","Confirmation",JOptionPane.YES_NO_OPTION);
      if(n==JOptionPane.YES_OPTION)
      {
          Release r = projet.getListeRelease().get(sel);
          if(r==projet.getReleaseActive())
          {
           projet.setReleaseActive(null);
            projet.getPresentation().update();
          }


      projet.getListeRelease().remove(sel);
          }
     updateliste();
      tbl_principal.repaint();
    }
    
    
    private void bt_activerActionPerformed(java.awt.event.ActionEvent evt) {                                             
   // updateliste();
         int sel = ligneSelectionnee();
         CRelease r= (CRelease)(projet.getListeRelease().get(sel));
         projet.setReleaseActive(r);
         updateliste();
         projet.getPresentation().update();
  //      projet.getPresentation().updateListeReleases();
    }

   public void updateliste(){
	   	   
	   md.removeAll();
	   Iterator<Release> it = projet.getListeRelease().iterator();
	   Release rl;
	   
	   while(it.hasNext()){
		   rl =(Release)it.next();
		   String [] t ={rl.getNom(),rl.getObjectif(),rl.getDateDebut().toString(),rl.getDateFin().toString()};
		   md.add(t);    
	   }
	   
	   mda.removeAll();
	   if(projet.getReleaseActive()!=null){
		   Release ra = projet.getReleaseActive();
		   String [] v ={ra.getNom(),ra.getObjectif(),ra.getDateDebut().toString(),ra.getDateFin().toString()};
		   mda.add(v);
        }
	    this.repaint();
	    tbl_principal.repaint();
	    if( projet.getPresentation()!=null)
	    projet.getPresentation().repaint();
   }//updateliste
                                        
   private void bt_detailActionPerformed(java.awt.event.ActionEvent evt) { 
		  int sel = ligneSelectionnee();
		  CRelease r= (CRelease)(projet.getListeRelease().get(sel));
		  if (r == (CRelease)projet.getReleaseActive()) {
			  JOptionPane.showMessageDialog(this, "Cette release est active, acc�dez � son d�tail via l'onglet Sprint");
		  }
		  else {
			    ////// popup_release popup = new popup_release(projet);
				base_affichage ba = new base_affichage(r.getPresentation());
				ba.getMainPanel().setLayout(new VerticalBagLayout());
				ba.getMainPanel().add(r.getPresentation());
				ba.setSize(800, 600);
				ba.setVisible(true);
		  }
	    }                                         

     private void bt_modifierActionPerformed(java.awt.event.ActionEvent evt) {                                             
        int sel = ligneSelectionnee();
          CRelease r= (CRelease)(projet.getListeRelease().get(sel));
        popup_release popup = new popup_release(projet,r);
        base_affichage ba = new base_affichage(popup);
        ba.getMainPanel().setLayout(new VerticalBagLayout());
        ba.getMainPanel().add(popup);
        ba.setVisible(true);
        
    }      
      public javax.swing.JTable getTable()
      {
          return  tbl_principal;
      }
    public int ligneSelectionnee(){
        return tbl_principal.getSelectedRow();
    }
    
    public void update(int sel){
     /*
        ((model_table_backlog)(tbl_principal.getModel())).update(sel, ((CBacklog)controle).getHistoire(sel).getNom(), ((CBacklog)controle).getHistoire(sel).getDescription(), ((CBacklog)controle).getHistoire(sel).getDuree(),((CBacklog)controle).getHistoire(sel)getTaches());
        tbl_principal.repaint();*/
    }
    
    public void updateAffichage(){
    	md.removeAll();  	
    	int i = projet.getListeRelease().size();
    	while (i>0){
    		i--;   		
			Release r = projet.getListeRelease().get(i);
    		String[] t = {r.getNom(), r.getObjectif(), String.valueOf(r.getDateDebut()), String.valueOf(r.getDateFin())};
    		md.add(t);
    		tbl_principal.repaint();
			
    	} 	
    	tbl_principal.repaint();
    }
    
    
    
    // Variables declaration - do not modify
    private JPanel panelVertical;

    private javax.swing.JButton bt_ajouter;
    private javax.swing.JButton bt_detail;
    private javax.swing.JButton bt_modifier;
    private javax.swing.JButton bt_monter;
    private javax.swing.JButton bt_suppr;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tbl_principal;
   private javax.swing.JTable tbl_active;
    // End of variables declaration                   

}
